
import React from "react";
import { Checkbox, Pagination } from 'tinper-bee';
import createYGCPage from '../../../components/CreateYGCPage/index.jsx';
import YGCupload from '../../../components/YGCCreateUpload';

import "./index.less";

import Table from "bee-table";
import 'bee-table/build/Table.css';
import multiSelect from "bee-table/build/lib/multiSelect.js";
let MultiSelectTable = multiSelect(Table, Checkbox);

import buttonClick from './buttonEvent.js';
import searchBtnClick from './searchBtnClick.js';

import { searchTpl } from './../tpl/searchTpl.js'
import { btnListTpl } from './../tpl/btnListTpl.js'
import { setTableTpl } from './../tpl/tableTpl.js'


import { ConstValues, getStopworkById } from './../projectIndicators.config.js';

class StopWorkList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            paginationItems: 1, // 分页总数
            activePage: 1, // 当前页
            tableData: [], // 表格数据
            selectRows: [], // 选择行
            fromGroupData: null, // 联查跳转数据
        };
        this.srcHeight = 60;
        this.columns = setTableTpl.call(this);
        this.initSearchData = window.sessionStorage.getItem('stopwork_search_area');
    }

    componentDidMount() {

    }

    initTplAfter = () => {
        // 企业看板跳转联查
        let fromGroupData = window.sessionStorage.getItem('fromGroupData');
        let stopworkFromCardBrowse = window.sessionStorage.getItem('stopworkFromCardBrowse');
        if (fromGroupData) {
            this.setState({ fromGroupData: JSON.parse(fromGroupData) }, () => {
                sessionStorage.removeItem('fromGroupData');
                let defaultSearchValue = this.state.fromGroupData;
                let { start_time, end_time, pkProject, projectName } = defaultSearchValue;
                this.props.search.setSearchValByField('pkProject', projectName, [{ pkProject: pkProject, sname: projectName }]);
                this.props.search.setSearchValByField('sectionDate', { startTime: this.props.vendorsFmtDate(start_time), endTime: this.props.vendorsFmtDate(end_time) });
                searchBtnClick.call(this, this.props.search.getSearchData().selectedS);
            })
        } else if (stopworkFromCardBrowse === 'true') {
            let stateData = JSON.parse(window.sessionStorage.getItem('stopWorkListData'));
            window.sessionStorage.removeItem('stopworkFromCardBrowse');
            window.sessionStorage.removeItem('stopWorkListData');
            this.setState(stateData);
            // if(this.initSearchData){
            //     JSON.parse(this.initSearchData);

            // }
        }
    }

    componentDidUpdate = (prevProps, prevState) => {

    };


    // 驳回、审批操作
    handlePassReject = (data) => {
        let { record, index } = data;
        let tableData = [...this.state.tableData];
        tableData[index] = record;
        this.setState({ tableData });
        this.props.passreject.closePassRejectModal();
    }

    // 表格单行点击
    tableRowClick = (record, index, event) => {
        getStopworkById.call(this, {
            record, callback: (res) => {
                let { resultCode, result, exMsg } = res.data;
                if (resultCode === 'ok') {
                    window.sessionStorage.setItem('stopWorkListData', JSON.stringify(this.state));
                    window.sessionStorage.setItem('pageStatus', 'browse');
                    window.sessionStorage.setItem('pageInfo', JSON.stringify(result));
                    window.location.href = './stopworkcard.html';
                } else {
                    this.props.vendorsDanger({ 'text': exMsg });
                }
            }
        })
    }

    // 存储选中行数据
    getSelectedDataFunc = (row) => {
        this.setState({ selectRows: row });
    }

    //分页搜索单据
    handleSelectPage = (e) => {
        let { selectedS } = this.props.search.getSearchData();
        this.setState({ activePage: e }, () => {
            let pageStart = 10 * (this.state.activePage - 1);
            selectedS.pageStart = pageStart;
            searchBtnClick.call(this, selectedS, { type: 'pagination' });
        });
    }

    render() {
        return (
            <div className="ygc-list">
                <div className='ygc-list-header-area'>
                    <div className='ygc-list-header-title'>停工单</div>
                    <div className='ygc-list-header-button'>
                        {this.props.YGCreateButton({
                            btnTpl: btnListTpl,
                            buttonClick: buttonClick.bind(this)
                        })}
                    </div>
                </div>
                <div className="ygc-search-Area">
                    {this.props.YGCCreateSearch(searchTpl, { searchBtnClick: searchBtnClick.bind(this) })}
                </div>
                <div className="ygc-table-Area">
                    <div className="ygc-projectIndicators-table">
                        <MultiSelectTable
                            className='ygc-stopwork-list-table'
                            columns={this.columns}
                            data={this.state.tableData}
                            showRowNum={true}
                            emptyText={() => '暂无数据'}
                            rowKey={this.setRowKey}
                            onRowDoubleClick={this.tableRowClick}
                            bordered
                            getSelectedDataFunc={this.getSelectedDataFunc}
                        />
                        {this.state.tableData.length !== 0 &&
                            <Pagination style={{ float: 'right', margin: '15px 0' }}
                                first
                                last
                                prev
                                next
                                boundaryLinks
                                items={this.state.paginationItems}
                                maxButtons={5}
                                activePage={this.state.activePage} // 尝试解决某些特殊环境下没有值得情况
                                onSelect={this.handleSelectPage}
                            />
                        }
                    </div>
                </div>
                {this.props.YGCreatePassRejectModal({ handlePassReject: this.handlePassReject })}
                {this.props.YGCreateApproveModal({})}
                {this.props.YGCreateAproHistoryModal({})}
                {this.state.selectRows.length !== 0 && <YGCupload
                    ref={(upload) => { this.ygcUpload = upload }}
                    billType={'TGBG'}
                    pkBill={this.state.selectRows[0][ConstValues.Primarykey]}
                    pkProject={this.state.selectRows[0]['pkProject']}
                />}
            </div>
        )
    }

    setRowKey = (record, index) => {
        return index
    };
}

export default createYGCPage({})(StopWorkList);
